//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for RssButton.
	/// </summary>
	public class RssButton : WebControl
	{
		//private HtmlImage image;
        private string imageUrl;

		#region Public Properties

		[DefaultValue( "" )]
		public virtual string ImageUrl
		{
			get
			{
				Object state = ViewState["ImageUrl"];
				if(state != null)
					return (string)state;
                return imageUrl;
			}
			set { ViewState["ImageUrl"] = value; }
		}

		[DefaultValue( "" )]
		public virtual string NavigateUrl
		{
			get
			{
				Object state = ViewState["NavigateUrl"];
				if(state != null)
					return (string)state;
				return string.Empty;
			}
			set { ViewState["NavigateUrl"] = value; }
		}

		[DefaultValue( 0 )]
		public virtual int Border
		{
			get
			{
				Object state = ViewState["Border"];
				if(state != null)
					return (int)state;
				return 0;
			}
			set { ViewState["Border"] = value; }
		}

	    /// <summary>
	    /// Include this feed in the header for auto discovery
	    /// </summary>
        [DefaultValue(false)]
        public virtual bool IncludeAutodiscovery
        {
            get
            {
                Object state = ViewState["IncludeAutodiscovery"];
                if (state != null)
                    return (bool)state;
                return false;
            }
            set { ViewState["IncludeAutodiscovery"] = value; }
        }

	    /// <summary>
	    /// the title to display for the url in IE7 Auto Feed Discovery
	    /// </summary>
	    public virtual string AutoDiscoveryTitle
	    {
            get
            {
                Object state = ViewState["AutoDiscoveryTitle"];
                if (state != null)
                    return (string)state;
                return NavigateUrl;
            }
            set { ViewState["AutoDiscoveryTitle"] = value; }
        }

		/// <summary>
		/// Should the button indicate via text that this is a secure feed
		/// </summary>
		[DefaultValue(false)]
		public virtual bool IsSecureFeed
		{
			get
			{
				Object state = ViewState["IsSecureFeed"];
				if (state != null)
					return (bool)state;
				return false;
			}
			set { ViewState["IsSecureFeed"] = value; }
		}


		#endregion

		#region Constructor

		public RssButton()
		{
			imageUrl = Globals.GetSiteUrls().RssImage(false);
		}

		#endregion

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (this.IncludeAutodiscovery && NavigateUrl != string.Empty)
				Head.AddRssAutoDiscovery(AutoDiscoveryTitle, Globals.FullPath(NavigateUrl), this.Context);

		}

		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			if(NavigateUrl != string.Empty)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);
				writer.RenderBeginTag(HtmlTextWriterTag.A);
			}
			
			writer.AddAttribute(HtmlTextWriterAttribute.Src, ImageUrl);
			writer.AddAttribute(HtmlTextWriterAttribute.Border, Border.ToString());

			if (IsSecureFeed)
				writer.AddAttribute(HtmlTextWriterAttribute.Alt, ResourceManager.GetString("ViewThreads_SecureXML"));
			else
				writer.AddAttribute(HtmlTextWriterAttribute.Alt, ResourceManager.GetString("ViewThreads_XML"));

			if(CssClass != "")
				writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
			if(Style != null)
			{
				string cssStyle = "";
				foreach(string key in Style.Keys)
					cssStyle += key + ":" + Style[key] + ";";

				writer.AddAttribute(HtmlTextWriterAttribute.Style, cssStyle);
			}

			writer.RenderBeginTag(HtmlTextWriterTag.Img);

			writer.RenderEndTag();

            if (NavigateUrl != string.Empty)
                writer.RenderEndTag();

		}

	}
}
